package cn.com.xy.sms.util;

import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
 
import android.content.Context;
import android.text.TextUtils;

/**
 * 
 * @author beanking
 *
 */
public class ParseMeizuManager {

    public final static String SMS_BALANCE = "1";// 余额
    public final static String SMS_FLOW = "2";// 流量
    
//    public final static String SMS_FLOW_TWO_OR_THREE="3"; //查2/3G流量
//    public final static String SMS_FLOW_THREE_OR_FOUR="4";//查3/4G流量
    public final static String SMS_FLOW_TWO="21";//查询2G流量
    public final static String SMS_FLOW_THREE="22";//查询3G流量
    public final static String SMS_FLOW_FOUR="23";// 查询4G流量 
    
    private final static String BALANCE_SEARCH = "查询余额";
    
    private final static String FLOW_SEARCH = "查询流量";
    private final static String FLOW_SEARCH_TWO_OR_THREE="查2/3G流量";
    private final static String FLOW_SEARCH_THREE_OR_FOUR="查3/4G流量";
    private final static String FLOW_SEARCH_TWO="查询2G流量";
    private final static String FLOW_SEARCH_THREE="查询3G流量";
    private final static String FLOW_SEARCH_FOUR="查询4G流量"; 
    
    private final static String RECHARGE_SEARCH = "查询充值";
    private final static String YW_SEARCH = "查询业务";

    /**
     * 查询短信指令
     * 
     * @param ctx
     * @param phoneNumber
     * @param orderType 指令类型 使用
     * @param numType
     * @param simIccid sim card's iccid
     * @param extend  extend param, is a Map<String,String>
     * @return actionData String
     * @throws JSONException throw a JSONException
     */
    public static String querySmsOrderActionData(Context ctx,
            String phoneNumber, String orderType, int numType, String simIccid,
            Map<String, String> extend) throws JSONException  {
        String actionData = "";
        String menuJson = ParseManager.queryMenuByPhoneNum(ctx, phoneNumber,
                numType, simIccid, extend);//get menu json string

        if (TextUtils.isEmpty(menuJson)) {
            return null;
        }

        JSONArray menuArr = new JSONArray(menuJson);

        for (int i = 0; i < menuArr.length(); i++) {
            JSONObject menuObj = menuArr.getJSONObject(i);
            if (RECHARGE_SEARCH.equals(menuObj.getString("name"))
                    || YW_SEARCH.equals(menuObj.getString("name"))) {
                JSONArray secondMenuArr = menuObj.getJSONArray("secondmenu");

                for (int j = 0; j < secondMenuArr.length(); j++) {
                    actionData = getSmsActionData(orderType,
                            secondMenuArr.getJSONObject(j));
                    if (!TextUtils.isEmpty(actionData)) {
                        return actionData;
                    }
                }

            } else {
                actionData = getSmsActionData(orderType, menuObj);
                if (!TextUtils.isEmpty(actionData)) {
                    return actionData;
                }
            }
        }

        return null;
    }

    /**
     * get JsonObject's actiondata value
     * 
     * @param orderType
     * @param menuObj menu button Obj
     * @return actiondata String
     * @throws JSONException
     */
    private static String getSmsActionData(String orderType, JSONObject menuObj)
            throws JSONException {
        if (SMS_BALANCE.equals(orderType)
                && BALANCE_SEARCH.equals(menuObj.getString("name"))) {
            return menuObj.getString("action_data");
        } else if ((SMS_FLOW.equals(orderType)||SMS_FLOW_TWO.equals(orderType)||SMS_FLOW_THREE.equals(orderType)||SMS_FLOW_FOUR.equals(orderType))
                && FLOW_SEARCH.equals(menuObj.getString("name"))) {
            return menuObj.getString("action_data");
        } else if(SMS_FLOW_TWO.equals(orderType)
                && (FLOW_SEARCH_TWO.equals(menuObj.getString("name"))||FLOW_SEARCH_TWO_OR_THREE.equals(menuObj.getString("name")))){
        	  return menuObj.getString("action_data");
        }else if(SMS_FLOW_THREE.equals(orderType)
                && (FLOW_SEARCH_THREE.equals(menuObj.getString("name"))||FLOW_SEARCH_TWO_OR_THREE.equals(menuObj.getString("name"))||FLOW_SEARCH_THREE_OR_FOUR.equals(menuObj.getString("name")))){
        	  return menuObj.getString("action_data");
        }else if(SMS_FLOW_FOUR.equals(orderType)
                && (FLOW_SEARCH_FOUR.equals(menuObj.getString("name"))||FLOW_SEARCH_THREE_OR_FOUR.equals(menuObj.getString("name")))){
        	  return menuObj.getString("action_data");
        }
        return null;
    }
}
